/*********************************************************
 * copyright learnren.com 版权所有 
 * 开发人员：ivan.yu
 * 创建时间：2013/4/28 16:47:07
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity;
using LEARNREN.COM.Entity.Enum;
using LEARNREN.COM.TOOL;
using LEARNREN.COM.UI;

namespace LEARNREN.COM.WebSite.Admin
{
    public partial class SiteConfigManager : BasePage
    {
        SiteLogic siteLogic = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowSiteConfig();
            }
        }

        private void ShowSiteConfig()
        {
            try
            {
                SiteConfig config = siteLogic.GetList().FirstOrDefault();
                if (config == null)
                {
                    btnSaveSiteConfig.CommandArgument = EditStateEnum.New.ToString(); ;
                }
                else
                {
                    btnSaveSiteConfig.CommandArgument = EditStateEnum.Edit.ToString();
                    this.SiteName.Text = config.SiteName;
                    this.KeyWords.Text = config.KeyWords;
                    this.Summary.Text = config.Summary;
                    this.CopyRight.Text = config.CopyRight;
                    this.TextBoxSiteUrl.Text = config.SiteUrl;
                    this.AdminMail.Text = config.AdminMail;
                    this.AdminMobile.Text = config.AdminMobile;
                    this.IsStaticEnable.Checked = config.IsStaticEnable;
                }
            }
            catch (Exception)
            {
                btnSaveSiteConfig.CommandArgument = string.Empty;
            }
        }

        private SiteConfig GetSiteConfigForSave()
        {
            SiteConfig config = null;

            if (btnSaveSiteConfig.CommandArgument == EditStateEnum.New.ToString())
            {
                config = new SiteConfig();
                config.Id = 0;
            }
            else
            {
                config = siteLogic.GetList((SiteConfig p) => true).FirstOrDefault();
            }


            config.KeyWords = KeyWords.Text;
            config.SiteName = SiteName.Text;
            config.Summary = Summary.Text;
            config.CopyRight = CopyRight.Text;
            config.SiteUrl = this.TextBoxSiteUrl.Text;
            config.AdminMail = this.AdminMail.Text;
            config.AdminMobile = this.AdminMobile.Text;
            config.IsStaticEnable = this.IsStaticEnable.Checked;

            return config;
        }
        
        protected void SaveSiteConfig_Click(object sender, EventArgs e)
        {
            try
            {
                SiteConfig config = GetSiteConfigForSave();
                siteLogic.Save(config);
                AddScript("showDialog('保存站点信息成功！');");
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
            }
        }

        protected override void InitLogic()
        {
            siteLogic = new SiteLogic();
        }

        protected override void DisposeLogic()
        {
            if (siteLogic != null)
            {
                siteLogic.Dispose();
            }
        }
    }
}